﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Framework.Utility;
using Sector7.Medical.Biz;
using Sector7.Medical.Consts;
using Sector7.Medical.Controls;
using Sector7.Medical.EntityType;
using Sector7.Medical.Events;
using Sector7.Medical.Utility;
using Sector7.Medical.Windows;
using Sector7.Medical.Enums;
using Sector7.Framework.Consts;

namespace Sector7.Medical.BIZM.Controls
{
    /// <summary>
    /// OutpatientControl.xaml 的交互逻辑
    /// </summary>
    public partial class OutpatientStation : SummaryControlBase
    {
        #region ============================================== Accessor
        #endregion

        #region ============================================== Public functions
        #endregion

        #region ============================================== Old Functions


        //protected override void btnAdd_Click(object sender, RoutedEventArgs e)
        //{
        //    //grdRecipeContainer.Children.Clear();

        //    //var now = DateTime.Now.ToString("yyyyMMdd");
        //    //int seed = Agent_SYSS.GetTernarySeed(C_TableName.RECIPE.ToString(),
        //    //    now,
        //    //    C_BillType.RECIPE_RECORD.ToString());
        //    //var recipe = new M7_Recipe();
        //    //recipe.RecipeId = Convert.ToInt64(PrimaryKeyUtil.GetPrimaryKey(
        //    //    C_TableName.RECIPE,
        //    //    now,
        //    //    C_BillType.RECIPE_RECORD, seed));

        //    //var recipeControl = new RecipeControl();
        //    //recipeControl.SetRecipe(recipe);

        //    //grdRecipeContainer.Children.Add(recipeControl);
        //    //grdRecipeContainer.IsEnabled = true;

        //    //var pageControl = grdRecipePage.Children[0] as PageControl;

        //    //pageControl.TurnedPage += new TurnedPageEventHandler(OnTurnPage);
        //    //int ret = pageControl.AppendNewPage();

        //    //base.btnAdd_Click(sender, e);
        //}

        //private void OnTurnPage(object obj, TurnedPageEventAgrs e)
        //{
        //    SelectRecipe(e.PageNo);
        //}

        //protected override void btnModify_Click(object sender, RoutedEventArgs e)
        //{
        //    //grdRecipeContainer.IsEnabled = true;
        //    //base.btnModify_Click(sender, e);
        //}

        //protected override void btnDelete_Click(object sender, RoutedEventArgs e)
        //{
        //    MessageBoxResult result = Alert.ShowDialog(AlertType.Question, "是否确认删除当前处方?");
        //    if (result == MessageBoxResult.Yes)
        //    {
        //        var __child = grdRecipeContainer.Children[0];
        //        if (__child is RecipeControl)
        //        {
        //            var recipeControl = __child as RecipeControl;
        //            m_recipeList.Remove(recipeControl.GetRecipe());

        //            var pageControl = grdRecipePage.Children[0] as PageControl;
        //            pageControl.TurnedPage += new TurnedPageEventHandler(OnTurnPage);
        //            int ret = pageControl.DeleteCurrentPage();
        //        }
        //    }

        //    base.btnDelete_Click(sender, e);
        //}

        //protected override void btnOk_Click(object sender, RoutedEventArgs e)
        //{
        //    var __child = grdRecipeContainer.Children[0];
        //    if (__child is RecipeControl)
        //    {
        //        var recipeControl = __child as RecipeControl;
        //        ClientUtil.UpdateToItems(m_recipeList, C_ColumnName.RECIPE_ID, recipeControl.GetRecipe());
        //    }

        //    grdRecipeContainer.IsEnabled = false;

        //    base.btnOk_Click(sender, e);
        //}

        //protected override void btnCancel_Click(object sender, RoutedEventArgs e)
        //{
        //    MessageBoxResult result = Alert.ShowDialog(AlertType.Question, "是否确认放弃当前未保存处方信息?");
        //    if (result == MessageBoxResult.Yes)
        //    {
        //        if (m_recipeList == null || m_recipeList.Count == 0)
        //        {
        //            grdRecipeContainer.Children.Clear();
        //            grdRecipeContainer.Children.Add(CreateLabel());
        //        }
        //    }
        //    base.btnCancel_Click(sender, e);
        //}

        //private void SelectRecipe(int pageNo)
        //{
        //    if (pageNo > 0)
        //    {
        //        var recipeItem = m_recipeList[pageNo - 1];
        //        var recipeItemControl = grdRecipeContainer.Children[0] as RecipeControl;
        //        recipeItemControl.SetRecipe(recipeItem);
        //    }
        //}

        //private void SwitchActionButtonVisibility(Visibility visibility)
        //{
        //    imgNew.Visibility = visibility;
        //    imgEdit.Visibility = visibility;
        //    imgDelete.Visibility = visibility;
        //    imgOk.Visibility = visibility;
        //    imgCancel.Visibility = visibility;
        //}

        private void grdActions_MouseEnter(object sender, MouseEventArgs e)
        {
            //SwitchActionButtonVisibility(Visibility.Visible);
        }

        private void grdActions_MouseLeave(object sender, MouseEventArgs e)
        {
            //SwitchActionButtonVisibility(Visibility.Hidden);
        }

        private void imgAction_MouseEnter(object sender, MouseEventArgs e)
        {
            var imgAction = sender as Image;
            imgAction.Cursor = Cursors.Hand;
        }

        private void imgAction_MouseLeave(object sender, MouseEventArgs e)
        {
            var imgAction = sender as Image;
            imgAction.Cursor = Cursors.Arrow;
        }

        #endregion

        #region ============================================== Private Functions

        /// <summary>
        /// 根据登记号类型获取病人信息
        /// </summary>
        /// <param name="registryNoType"></param>
        /// <param name="registryNo"></param>
        private void LoadPatientByRegistryNo(RegistryNoType registryNoType, object registryNo)
        {
            var patient = Agent_BIZS.GetPatientByRegistryNo(registryNoType, registryNo);

            if (patient == null)
            {
                Alert.ShowDialog(AlertType.Error, "无法找到登记号 {0}-{1} 对应病人信息！", registryNoType, registryNo);
            }
            else
            {
                LoadPatientToInterface(patient);
            }
        }

        /// <summary>
        /// 向界面加载病人信息
        /// </summary>
        /// <param name="patient"></param>
        private void LoadPatientToInterface(M7_Patient patient)
        {
            m_patient = patient;
            m_medicalRecordList = Agent_BIZS.GetRecentTenMedicalRecordList(patient.CitizenId);

            patientCard.LoadPatient(m_patient);

            txbRegistryNo.Text = string.Empty;
        }

        /// <summary>
        /// 创建lblRecipeRemarks标签
        /// </summary>
        private Label CreateLabel()
        {
            var label = new Label();
            label.Content = "请点击下方『添加』按钮开始创建处方单.";
            label.Margin = new Thickness(0, 10, 0, 10);
            label.Name = "lblRecipeRemarks";

            return label;
        }

        #endregion

        #region ============================================== Event Functions
        /// <summary>
        /// 加载控件事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SummaryControlBase_Loaded(object sender, RoutedEventArgs e)
        {
        }

        /// <summary>
        /// F7：根据身份证号码读取病人信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnF7_Click(object sender, RoutedEventArgs e)
        {
            LoadPatientByRegistryNo(RegistryNoType.IdentityCardId, txbRegistryNo.Text);
        }

        /// <summary>
        /// F8：根据医疗卡读取病人信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnHealthCard_Click(object sender, RoutedEventArgs e)
        {
            string healthCardId = RfReaderUtil.GetSerialNumber();
            if (string.IsNullOrWhiteSpace(healthCardId))
            {
                Alert.ShowDialog(AlertType.Error, "读取医疗卡号失败！");
            }
            else
            {
                txbRegistryNo.Text = healthCardId;
                LoadPatientByRegistryNo(RegistryNoType.HealthCardId, healthCardId);
            }
        }

        /// <summary>
        /// F12：新开卡，添加病人信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddPatient_Click(object sender, RoutedEventArgs e)
        {
            var addPatientWindow = new AddPatientWindow();
            addPatientWindow.InitializeWindow(CrudType.Create);
            addPatientWindow.AddSuccess += new EventHandler(AddPatientWindow_AddSuccess);

            addPatientWindow.ShowDialog();
        }

        /// <summary>
        /// 添加病人信息成功
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddPatientWindow_AddSuccess(object sender, EventArgs e)
        {
            var addPatientWindow = sender as AddPatientWindow;
            var patient = Agent_BIZS.GetPatientByRegistryNo(RegistryNoType.CitizenId, addPatientWindow.CitizenId);

            LoadPatientToInterface(patient);
        }

        /// <summary>
        /// 点击添加处方按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddRecipe_Click(object sender, RoutedEventArgs e)
        {
            var now = DateTime.Now.ToString("yyyyMMdd");
            int seed = Agent_SYSS.GetTernarySeed(C_TableName.RECIPE.ToString(),
                now,
                C_BillType.RECIPE_RECORD.ToString());

            var recipe = new M7_Recipe();
            recipe.RecipeId = Convert.ToInt64(PrimaryKeyUtil.GetPrimaryKey(
                C_TableName.RECIPE,
                now,
                C_BillType.RECIPE_RECORD, seed));

            var recipeControl = new RecipeControl();
            recipeControl.SetRecipe(recipe);
            recipeControl.Width = 450;
            recipeControl.Height = 345;

            wrpRecipeContainer.Children.Add(recipeControl);
        }

        /// <summary>
        /// 点击确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            var recipeList = new List<M7_Recipe>();

            foreach (RecipeControl recipeControl in wrpRecipeContainer.Children)
            {
                var recipe = recipeControl.GetRecipe();

                recipeList.Add(recipe);
            }

            //Agent_BIZS.AddRecipeList(recipeList);
        }

        #endregion

        #region ============================================== Structrue
        public OutpatientStation()
        {
            InitializeComponent();
        }
        #endregion

        #region ============================================== Inner Member
        private M7_Patient m_patient = null;
        private List<M7_MedicalRecord> m_medicalRecordList = null;
        private List<M7_Recipe> m_recipeList = null;
        #endregion
    }
}
